package models

import (
	_ "time"

	orm "go-papers/database"
)

type OrganBrand struct {
	BrandId                int    `json:"brandId" gorm:"type:int(11) unsigned;primary_key"` //
	OrganId                int    `json:"organId" gorm:"type:int(11);"`                     // 机构
	OrganName              string `json:"organName" gorm:"type:varchar(255);"`              // 机构名称
	BrandLogo              string `json:"brandLogo" gorm:"type:varchar(255);"`              // 品牌logo
	BrandImage             string `json:"brandImage" gorm:"type:varchar(255);"`             // 品牌名称
	BrandName              string `json:"brandName" gorm:"type:varchar(255);"`              // 品牌名称
	BrandRegister          string `json:"brandRegister" gorm:"type:varchar(32);"`           // 成立时间
	BrandCompany           string `json:"brandCompany" gorm:"type:varchar(255);"`           // 品牌公司名称
	BrandQualify           string `json:"brandQualify" gorm:"type:varchar(255);"`           // 办学资质
	BrandCapital           string `json:"brandCapital" gorm:"type:varchar(255);"`           // 环境安全
	BrandFina              string `json:"brandFina" gorm:"type:varchar(255);"`              // 财务授权 图片
	BrandFinancing         string `json:"brandFinancing" gorm:"type:varchar(255);"`         // 融资情况
	BrandExcellentBusiness string `json:"brandExcellentBusiness" gorm:"type:varchar(255);"` // 优秀业绩
	BrandCourseNums        int    `json:"brandCourseNums" gorm:"type:decimal(10,0);"`       // 课程数量 ---@TODO 计算
	BrandOrderNums         int    `json:"brandOrderNums" gorm:"type:decimal(10,0);"`        // 订单数  ---- @TODO 计算
	BrandDevelopPower      string `json:"brandDevelopPower" gorm:"type:text;"`              // 研发能力
	BrandInfo              string `json:"brandInfo" gorm:"type:text;"`                      // 介绍图片 json
	BrandTeacher           string `json:"brandTeacher" gorm:"type:varchar(255);"`           // 教师体系材料
	BrandBusinessLicense   string `json:"brandBusinessLicense" gorm:"type:varchar(255);"`   // 营业执照
	CreateBy               string `json:"createBy" gorm:"type:varchar(32);"`                // 创建
	UpdateBy               string `json:"updateBy" gorm:"type:varchar(32);"`                // 更新
	CreateByName           string `json:"createByName" gorm:"type:varchar(255);"`           // 创建人
	UpdateByName           string `json:"updateByName" gorm:"type:varchar(255);"`           // 更新人

	ApprovalStatus   string `json:"approvalStatus" gorm:"type:varchar(255);"`   // 核准状态 1 待审核  3 失败 5 已通过
	ApprovalAt       string `json:"approvalAt" gorm:"type:varchar(255);"`       // 核准时间
	ApprovalUserId   string `json:"approvalUserId" gorm:"type:varchar(32);"`    // 核准人用户id
	ApprovalUserName string `json:"approvalUserName" gorm:"type:varchar(255);"` // 审核核准人名称

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`

	UpdateStart string `json:"updateStart" gorm:"-"` // 跟新 起始时间
	UpdateEnd   string `json:"updateEnd" gorm:"-"`   // 跟新 结束时间

	BaseModel
}

type OrganBrandAll struct {
	BrandId   int    `json:"brandId" gorm:"type:int(11) unsigned;primary_key"` //
	BrandLogo string `json:"brandLogo" gorm:"type:varchar(255);"`              // 品牌logo
	BrandName string `json:"brandName" gorm:"type:varchar(255);"`              // 品牌名称
}

func (OrganBrand) TableName() string {
	return "organ_brand"
}

// 创建OrganBrand
func (e *OrganBrand) Create() (OrganBrand, error) {
	var doc OrganBrand
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *OrganBrand) GetByID(id int) (OrganBrand, error) {
	var doc OrganBrand
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("brand_id = ?", id)
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrganBrand
func (e *OrganBrand) Get() (OrganBrand, error) {
	var doc OrganBrand
	table := orm.Eloquent.Table(e.TableName())
	if e.BrandId != 0 {
		table = table.Where("brand_id = ?", e.BrandId)
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.BrandRegister != "" {
		table = table.Where("brand_register = ?", e.BrandRegister)
	}
	if e.BrandCompany != "" {
		table = table.Where("brand_company like ?", "%"+e.BrandCompany+"%")
	}
	if e.BrandQualify != "" {
		table = table.Where("brand_qualify = ?", e.BrandQualify)
	}
	if e.BrandFinancing != "" {
		table = table.Where("brand_financing = ?", e.BrandFinancing)
	}
	if e.CreateBy != "" {
		table = table.Where("create_by like ?", "%"+e.CreateBy+"%")
	}
	if e.UpdateBy != "" {
		table = table.Where("update_by = ?", e.UpdateBy)
	}
	if e.ApprovalStatus != "" {
		table = table.Where("approval_status = ?", e.ApprovalStatus)
	}
	if e.ApprovalAt != "" {
		table = table.Where("approval_at = ?", e.ApprovalAt)
	}
	if e.ApprovalUserId != "" {
		table = table.Where("approval_user_id = ?", e.ApprovalUserId)
	}
	if e.ApprovalUserName != "" {
		table = table.Where("approval_user_name like ?", "%"+e.ApprovalUserName+"%")
	} //data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

//
func (e *OrganBrand) GetPageAll() ([]OrganBrandAll, error) {
	var doc []OrganBrandAll

	table := orm.Eloquent.Select("brand_id,brand_name,brand_logo").Table(e.TableName())
	if e.BrandId != 0 {
		table = table.Where("brand_id = ?", e.BrandId)
	} else {
		table = table.Where("brand_id >= 10")
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.BrandRegister != "" {
		table = table.Where("brand_register = ?", e.BrandRegister)
	}
	if e.BrandCompany != "" {
		table = table.Where("brand_company like ?", "%"+e.BrandCompany+"%")
	}
	if e.BrandQualify != "" {
		table = table.Where("brand_qualify = ?", e.BrandQualify)
	}
	if e.BrandFinancing != "" {
		table = table.Where("brand_financing = ?", e.BrandFinancing)
	}
	if e.CreateBy != "" {
		table = table.Where("create_by like ?", "%"+e.CreateBy+"%")
	}
	if e.UpdateBy != "" {
		table = table.Where("update_by = ?", e.UpdateBy)
	}
	if e.ApprovalStatus != "" {
		table = table.Where("approval_status = ?", e.ApprovalStatus)
	} else {
		table = table.Where("approval_status = 5")
	}

	if e.ApprovalAt != "" {
		table = table.Where("approval_at = ?", e.ApprovalAt)
	}
	if e.ApprovalUserId != "" {
		table = table.Where("approval_user_id = ?", e.ApprovalUserId)
	}
	if e.ApprovalUserName != "" {
		table = table.Where("approval_user_name like ?", "%"+e.ApprovalUserName+"%")
	}

	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	if err := table.Find(&doc).Error; err != nil {
		return nil, err
	}
	return doc, nil
}

// 获取OrganBrand带分页
func (e *OrganBrand) GetPage(pageSize int, pageIndex int) ([]OrganBrand, int, error) {
	var doc []OrganBrand

	table := orm.Eloquent.Select("*").Table(e.TableName())

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "organ_brand", e.DataScope, "机构品牌列表")

	if e.BrandId != 0 {
		table = table.Where("brand_id = ?", e.BrandId)
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.BrandRegister != "" {
		table = table.Where("brand_register = ?", e.BrandRegister)
	}
	if e.BrandCompany != "" {
		table = table.Where("brand_company like ?", "%"+e.BrandCompany+"%")
	}
	if e.BrandQualify != "" {
		table = table.Where("brand_qualify = ?", e.BrandQualify)
	}
	if e.BrandFinancing != "" {
		table = table.Where("brand_financing = ?", e.BrandFinancing)
	}
	if e.CreateBy != "" {
		table = table.Where("create_by like ?", "%"+e.CreateBy+"%")
	}
	if e.UpdateBy != "" {
		table = table.Where("update_by = ?", e.UpdateBy)
	}
	if e.ApprovalStatus != "" {
		table = table.Where("approval_status = ?", e.ApprovalStatus)
	}
	if e.ApprovalAt != "" {
		table = table.Where("approval_at = ?", e.ApprovalAt)
	}
	if e.ApprovalUserId != "" {
		table = table.Where("approval_user_id = ?", e.ApprovalUserId)
	}
	if e.ApprovalUserName != "" {
		table = table.Where("approval_user_name like ?", "%"+e.ApprovalUserName+"%")
	}

	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrganBrand
func (e *OrganBrand) Update(id int) (update OrganBrand, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("brand_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrganBrand
func (e *OrganBrand) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("brand_id = ?", id).Delete(&OrganBrand{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrganBrand) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("brand_id in (?)", id).Delete(&OrganBrand{}).Error; err != nil {
		return
	}
	Result = true
	return
}
